package com.semlab.client.mvp.home.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.semlab.client.proxy.MovieProxy;

public abstract class MovieCell extends FocusPanel {

    private static MovieCellUiBinder uiBinder = GWT.create(MovieCellUiBinder.class);

    interface MovieCellUiBinder extends UiBinder<Widget, MovieCell> {
    }

    private MovieProxy movie;

    @UiField
    MyStyle style;

    interface MyStyle extends CssResource {
        String selected();

        String desc();

        String details();

        String detailsLink();

        String cell();

        String images();

        String rating();

        String container();
    }

    @UiField
    Label movieTitle;
    @UiField
    Label movieRating;

    @UiField
    Image movieImage;
    @UiField
    Image fbIco;
    @UiField
    Image wikiIco;
    @UiField
    Image imdbIco;
    @UiField
    Image graphIco;
    @UiField
    FocusPanel detailsButton;

    public MovieCell(MovieProxy movie) {
        setWidget(uiBinder.createAndBindUi(this));
        setMovie(movie);
        addMouseOverHandler(new MouseOverHandler() {

            @Override
            public void onMouseOver(MouseOverEvent event) {
                detailsButton.getElement().getStyle().setDisplay(Style.Display.INLINE_BLOCK);
            }
        });
        addMouseOutHandler(new MouseOutHandler() {
            @Override
            public void onMouseOut(MouseOutEvent event) {
//                if(!selected){
                    detailsButton.getElement().getStyle().setDisplay(Style.Display.NONE);
//                }
            }
        });
        setStyleName(style.container());
    }

    private void setMovie(final MovieProxy movie) {
        if (movie != null) {
            this.movie = movie;
            movieTitle.setText(movie.getTitle());
            if (movie.getImdbUri() != null) {
                imdbIco.setVisible(true);
            }
            if (!movie.getWikiUri().isEmpty()) {
                wikiIco.setVisible(true);
                if (!movie.isEnriched()) {
                    graphIco.setVisible(true);
                }
            }

            if (movie.getImageUrl() != null) {
                movieImage.setUrl(movie.getImageUrl()); // TODO set default
                // image
            }

            if (movie.getRating() != null) {
                movieRating.setText("(" + String.valueOf(movie.getRating()) + ")");
            }
        }
    }

    @UiHandler("fbIco")
    public void handleClick1(ClickEvent event) {
        Window.open(movie.getFbUri(), "_blank", "");
    }


    @UiHandler("wikiIco")
    public void handleClick2(ClickEvent event) {
        Window.open(movie.getWikiUri(), "_blank", "");
    }


    @UiHandler("graphIco")
    public void handleClick3(ClickEvent event) {
        enrichMovieData(movie);
        graphIco.setVisible(false);
    }


    @UiHandler("imdbIco")
    public void handleClick4(ClickEvent event) {
        Window.open(movie.getImdbUri(), "_blank", "");
    }

    @UiHandler({"detailsButton","movieTitle"})
    public void handleClick(ClickEvent event) {
        showDetails(movie);
    }

    public abstract void enrichMovieData(MovieProxy movie);

    public abstract void showDetails(MovieProxy movie);

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        graphIco.addClickHandler(handler);
        detailsButton.addClickHandler(handler);
        return movieTitle.addClickHandler(handler);
    }

    public void setSelected(boolean selected){
        if (selected) {
            getElement().addClassName(style.selected());
        }else {
//            detailsButton.getElement().getStyle().setDisplay(Style.Display.NONE);
            getElement().removeClassName(style.selected());
        }
    }

}
